#include <stdio.h>
#include <unistd.h>
#include <linux/input.h>

const char* beeper_path = "/dev/input/by-path/platform-beeper-event";

int tone(int hz)
{
    //打开设备文件
    FILE* fp = fopen(beeper_path, "w");
    if (!fp)
    {
        perror(beeper_path);
        return 1;
    }

    struct input_event event;
    //设备类型
    event.type = EV_SND;
    //命令
    event.code = SND_TONE;
    //命令参数
    event.value = hz;

    fwrite(&event, sizeof event, 1, fp);

    fclose(fp);
    return 0;
}

int main()
{
    while(1)
    {
        tone(1000);
        sleep(1);
        tone(0);
        sleep(1);
    }

    return 0;
}
